/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*/

package br.ufmg.dees.insane.model;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ListIterator;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import br.ufmg.dees.insane.crossSection.CrossSection;
import br.ufmg.dees.insane.crossSection.RegCrossSection;
import br.ufmg.dees.insane.drivers.Driver;
import br.ufmg.dees.insane.drivers.Step;
import br.ufmg.dees.insane.drivers.StructuralMech;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.materialMedia.material.Material;
import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.analysismodel.AxiSymetricAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.Line_1D;
import br.ufmg.dees.insane.model.disc.analysismodel.Line_2D;
import br.ufmg.dees.insane.model.disc.analysismodel.Line_3D;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStrainAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStressAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.SolidAnalysisM;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ4;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ8;
import br.ufmg.dees.insane.model.disc.element.ElmAxiQ9;
import br.ufmg.dees.insane.model.disc.element.ElmH20;
import br.ufmg.dees.insane.model.disc.element.ElmH8;
import br.ufmg.dees.insane.model.disc.element.ElmL2;
import br.ufmg.dees.insane.model.disc.element.ElmL3;
import br.ufmg.dees.insane.model.disc.element.ElmL4;
import br.ufmg.dees.insane.model.disc.element.ElmQ4;
import br.ufmg.dees.insane.model.disc.element.ElmQ8;
import br.ufmg.dees.insane.model.disc.element.ElmQ9;
import br.ufmg.dees.insane.model.disc.element.ElmT10;
import br.ufmg.dees.insane.model.disc.element.ElmT3;
import br.ufmg.dees.insane.model.disc.element.ElmT6;
import br.ufmg.dees.insane.model.disc.element.ElmTetra4;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.element.force.ElementForce;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationorder.IntegrationOrder;
import br.ufmg.dees.insane.model.disc.integrationpoint.IntegrationPoint;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.model.disc.shape.H20;
import br.ufmg.dees.insane.model.disc.shape.H8;
import br.ufmg.dees.insane.model.disc.shape.L2;
import br.ufmg.dees.insane.model.disc.shape.L3;
import br.ufmg.dees.insane.model.disc.shape.L4;
import br.ufmg.dees.insane.model.disc.shape.Q4;
import br.ufmg.dees.insane.model.disc.shape.Q8;
import br.ufmg.dees.insane.model.disc.shape.Q9;
import br.ufmg.dees.insane.model.disc.shape.Shape;
import br.ufmg.dees.insane.model.disc.shape.T10;
import br.ufmg.dees.insane.model.disc.shape.T3;
import br.ufmg.dees.insane.model.disc.shape.T6;
import br.ufmg.dees.insane.model.disc.shape.Tetra4;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IPoint3d;
import br.ufmg.dees.insane.util.IVector;


/**
*A class containing the driver for a structural mech problem.
*It contains the main class of the JMef application.
*
*@author Fonseca, Flavio & Lucas, Marcelo & Pitangueira, Roque
*@version 1.0
*@since June 2004
*/

public class JMef implements java.io.Serializable
{
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	/** The Driver of this application. */
	protected  static Driver driver;
	protected  static Step   step;
	
	/** The Object which will be serialized. */
	protected static Object docOutput;
	
//*****************************************************************************
	
	/** Returns the Driver of this application.
	*@return The Driver of this application.
	*/
	public Driver getDriver()
	{
		return (driver);
	}
	
	/** Sets the Driver of this application.
	*@param a The Driver of this application.
	*/
	public void setDriver(StructuralMech a)
	{
		driver = a;
	}
	
//*****************************************************************************
	
	/** The main class of this application.
	*@param arguments The arguments for this main class (Not used).
	*/
	public static void main(String[] arguments)
	{
		buildDriverFromXML(arguments[0]);
		//step = new StandardNewtonRapson(assembler, 1, 0.0,(byte)2);
		
		//driver.analyseModel();
		
		AnalysisModel anl = driver.getFemModel().getGlobalAnalysisModel();
		anl.printDisplacements(driver.getFemModel());
		anl.printReactions(driver.getFemModel());
		
		buildDOMTreeFromDriver();
		String outPutFileName = "outPut"+arguments[0];
		serializeXMLDocument(outPutFileName);
	}
	
//*****************************************************************************
	
	/** Builds the driver from the data in a XML file.
	*@param XMLFile The name of the input XML file.
	*/
	public static void buildDriverFromXML(String XMLFile)
	{
	//Creating and configuring the SAXParser
	SAXParserFactory parserFac = SAXParserFactory.newInstance();
	parserFac.setValidating(true);
	try
	{
		SAXParser parser = parserFac.newSAXParser();
		
		//implementing DefaultHandler
		parser.parse(XMLFile, new DefaultHandler()
		{
			StringBuffer content = new StringBuffer();
			
			//All the classes which will be instantiated are declared here
			
			FemModel femModel;
//			Solution solution;
			Node node;
			IPoint3d coord;
			ParametricElement elm;
			Shape shp;
			LinearElasticIsotropic matIso;
			CrossSection cs;
			AnalysisModel anl;
			IntegrationOrder intOr;
			ElementForce ef;
			PointForce pf;
			StringTokenizer stk;
			
			int matType; //1 for Isotropic, 2 for MSpring
			
			public void setDocumentLocator(Locator locator)
			{
			}
			
			public void characters(char ch[], int start, int length)
			{
				this.content.append(ch, start, length);
			}
			
	//todos os ifs deste metodo serao ativados quando o parser encontrar <....>
			public void startElement(String uri, String localName, String qName, Attributes attributes)
			{
				//Read "<Model ...>"
				if ("Model".equals(qName))
				{
					femModel = new FemModel();
				}
				
				//Read "<NodeList ...>"
				else if ("NodeList".equals(qName))
				{
				}
				
				//Read "<MaterialList ...>"
				else if ("MaterialList".equals(qName))
				{
				}
				
				//Read "<CrossSectionList ...>"
				else if ("CrossSectionList".equals(qName))
				{
				}
				
				//Read "<ElementList ...>"
				else if ("ElementList".equals(qName))
				{
				}
				
				//Read "<Node ...>"
				else if ("Node".equals(qName))
				{
					node = new Node();
					node.setLabel(attributes.getValue("label"));
				}
				
				//Read "<Material ...>"
				else if ("Material".equals(qName))
				{
					if (attributes.getValue("class").equals("LinearElasticIsotropic"))
					{
						 matIso = new LinearElasticIsotropic();
						 matType = 1;
					}
					matIso.setLabel(attributes.getValue("label"));
				}
				
				//Read "<CrossSection ...>"
				else if ("CrossSection".equals(qName))
				{
					cs = new RegCrossSection();
					cs.setLabel(attributes.getValue("label"));
				}
				
				//Read "<Element ...>"
				else if ("Element".equals(qName))
				{
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.Q4"))
					{
						shp = new Q4();
						femModel.add(shp);
						elm = new ElmQ4(femModel.getShape("Q4"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.Q8"))
					{
						shp = new Q8();
						femModel.add(shp);
						elm = new ElmQ8(femModel.getShape("Q8"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.Q9"))
					{
						shp = new Q9();
						femModel.add(shp);
						elm = new ElmQ9(femModel.getShape("Q9"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Triangular.T3"))
					{
						shp = new T3();
						femModel.add(shp);
						elm = new ElmT3(femModel.getShape("T3"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Triangular.T6"))
					{
						shp = new T6();
						femModel.add(shp);
						elm = new ElmT6(femModel.getShape("T6"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Triangular.T10"))
					{
						shp = new T10();
						femModel.add(shp);
						elm = new ElmT10(femModel.getShape("T10"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Hexahedral.H8"))
					{
						shp = new H8();
						femModel.add(shp);
						elm = new ElmH8(femModel.getShape("H8"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Hexahedral.H20"))
					{
						shp = new H20();
						femModel.add(shp);
						elm = new ElmH20(femModel.getShape("H20"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Tetrahedral.Tetra4"))
					{
						shp = new Tetra4();
						femModel.add(shp);
						elm = new ElmTetra4(femModel.getShape("Tetra4"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.AxiQ4"))
					{
						shp = new Q4();
						femModel.add(shp);
						elm = new ElmAxiQ4(femModel.getShape("Q4"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.AxiQ8"))
					{
						shp = new Q8();
						femModel.add(shp);
						elm = new ElmAxiQ8(femModel.getShape("Q8"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Quadrilateral.AxiQ9"))
					{
						shp = new Q9();
						femModel.add(shp);
						elm = new ElmAxiQ9(femModel.getShape("Q9"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Bar.L2"))
					{
						shp = new L2();
						femModel.add(shp);
						elm = new ElmL2(femModel.getShape("L2"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Bar.L3"))
					{
						shp = new L3();
						femModel.add(shp);
						elm = new ElmL3(femModel.getShape("L3"));
					}
					if (attributes.getValue("class").equals("ParametricElement.Bar.L4"))
					{
						shp = new L4();
						femModel.add(shp);
						elm = new ElmL4(femModel.getShape("L4"));
					}
					elm.setLabel(attributes.getValue("label"));
				}
				
				//Read "<Coord ...>"
				else if ("Coord".equals(qName))
				{
					coord = new IPoint3d();
				}
				
				//Read "<Restraints ...>"
				else if ("Restraints".equals(qName))
				{
				}
				
				//Read "<Load ...>"
				else if ("Load".equals(qName))
				{
				}
				
				//Read "<PreDisplacements ...>"
				else if ("PreDisplacements".equals(qName))
				{
				}
				
				//Read "<Elasticity ...>"
				else if ("Elasticity".equals(qName))
				{
				}
				
				//Read "<Poisson ...>"
				else if ("Poisson".equals(qName))
				{
				}
				
				//Read "<Area ...>"
				else if ("Area".equals(qName))
				{
				}
				
				//Read "<Thickness>"
				else if ("Thickness".equals(qName))
				{
					cs = new RegCrossSection();
				}
				
				//Read "<Ix ...>"
				else if ("Ix".equals(qName))
				{
				}
				
				//Read "<Incidence ...>"
				else if ("Incidence".equals(qName))
				{
				}
				
				//Read "<IntegrationOrder>"
				else if ("IntegrationOrder".equals(qName))
				{
				}
				
				//Read "<MyMaterial ...>"
				else if ("MyMaterial".equals(qName))
				{
				}
				
				//Read "<MyCrossSection ...>"
				else if ("MyCrossSection".equals(qName))
				{
				}
				
				//Read "<MyAnalysisModel ...>"
				else if ("MyAnalysisModel".equals(qName))
				{
				}
				
				//Read "<Liberations ...>"
				else if ("Liberations".equals(qName))
				{
				}
				
				//Read "<MyPointForce ...>"
				else if ("MyPointForce".equals(qName))
				{
					pf = new PointForce();
				}
				
				//Read "<MyLineForce ...>"
				else if ("MyLineForce".equals(qName))
				{
				}
				
				//Read "<MySurfaceForce ...>"
				else if ("MySurfaceForce".equals(qName))
				{
				}
				
				//Read "<MyBodyForce ...>"
				else if ("MyBodyForce".equals(qName))
				{
				}
				
				//Read "<LineElementForce ...>"
				else if ("LineElementForce".equals(qName))
				{
					ef = new ElementForce();
System.out.println("instanciou o LineelementForce");
				}
				
				//Read "<SurfaceElementForce ...>"
				else if ("SurfaceElementForce".equals(qName))
				{
					ef = new ElementForce();
System.out.println("instanciou o SurfaceelementForce");
				}
				
				//Read "<VolumeElementForce ...>"
				else if ("VolumeElementForce".equals(qName))
				{
					ef = new ElementForce();
System.out.println("instanciou o VolumeelementForce");
				}
				
				//Read "<PointForce ...>"
				else if ("PointForce".equals(qName))
				{
					pf = new PointForce();
				}
				
				//Read "<PointForceCoords ...>"
				else if ("PointForceCoords".equals(qName))
				{
					coord = new IPoint3d();
				}
				
				//Read "<Force ...>"
				else if ("Force".equals(qName))
				{
				}
				
				//Read "<GlobalAnalysisModel ...>"
				else if ("GlobalAnalysisModel".equals(qName))
				{
				}
				//Read "<Driver ...>"
				else if ("Driver".equals(qName))
				{
				}
				
				//Read "<Solution ...>"
				else if ("Solution".equals(qName))
				{
				}
				
			} //colchete do starElement
			
//todos os ifs deste metodo serao ativados quando o parser encontrar </....>
			public void endElement(String uri, String localName, String qName)
			{
				//Read "</Model>"
				if ("Model".equals(qName))
				{
				}
				
				//Read "</NodeList>"
				else if ("NodeList".equals(qName))
				{
				}
				
				//Read "</MaterialList>"
				else if ("MaterialList".equals(qName))
				{
				}
				
				//Read "</CrossSectionList>"
				else if ("CrossSectionList".equals(qName))
				{
				}
				
				//Read "</ElementList>"
				else if ("ElementList".equals(qName))
				{
				}
				
				//Read "</Node>"
				else if ("Node".equals(qName))
				{
					femModel.add(node);
				}
				
				//Read "</Material>"
				else if ("Material".equals(qName))
				{
					if (matType==1)
						femModel.add(matIso);
//					if (matType==2)
//						femModel.add(matSpr);
						
				}
				
				//Read "</CrossSection>"
				else if ("CrossSection".equals(qName))
				{
					femModel.add(cs);
				}
				
				//Read "</Element>"
				else if ("Element".equals(qName))
				{
//					elm.mountEquivNodalLoad();
					femModel.add(elm);
				}
				
				//Read "</Coord>"
				else if ("Coord".equals(qName))
				{
//StringTokenizer e apenas um quebrador de strings
					stk = new StringTokenizer(content.toString()," ");
					coord.x = Double.parseDouble(stk.nextToken());
					coord.y = Double.parseDouble(stk.nextToken());
//					if (stk.hasMoreTokens())
						coord.z = Double.parseDouble(stk.nextToken());
					node.setCoord(coord);
				}
				
				//Read "</Restraints>"
				else if ("Restraints".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(0, true);
					else node.setRestraint(0, false);
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(1, true);
					else node.setRestraint(1, false);
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(2, true);
					else node.setRestraint(2, false);
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(3, true);
					else node.setRestraint(3, false);
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(4, true);
					else node.setRestraint(4, false);
					
					if (stk.nextToken().toLowerCase().equals("true"))
						node.setRestraint(5, true);
					else node.setRestraint(5, false);
				}
				
				//Read "</Load>"
				else if ("Load".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					node.setForce(0, Double.parseDouble(stk.nextToken()));
					node.setForce(1, Double.parseDouble(stk.nextToken()));
					node.setForce(2, Double.parseDouble(stk.nextToken()));
					node.setForce(3, Double.parseDouble(stk.nextToken()));
					node.setForce(4, Double.parseDouble(stk.nextToken()));
					node.setForce(5, Double.parseDouble(stk.nextToken()));
				}
				
				//Read "</PreDisplacements>"
				else if ("PreDisplacements".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					node.setPreDisplacement(0, Double.parseDouble(stk.nextToken()));
					node.setPreDisplacement(1, Double.parseDouble(stk.nextToken()));
					node.setPreDisplacement(2, Double.parseDouble(stk.nextToken()));
					node.setPreDisplacement(3, Double.parseDouble(stk.nextToken()));
					node.setPreDisplacement(4, Double.parseDouble(stk.nextToken()));
					node.setPreDisplacement(5, Double.parseDouble(stk.nextToken()));
				}
				
				//Read "</mySpring>"
				else if ("mySpring".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					node.setSpring(0, Double.parseDouble(stk.nextToken()));
					node.setSpring(1, Double.parseDouble(stk.nextToken()));
					node.setSpring(2, Double.parseDouble(stk.nextToken()));
					node.setSpring(3, Double.parseDouble(stk.nextToken()));
					node.setSpring(4, Double.parseDouble(stk.nextToken()));
					node.setSpring(5, Double.parseDouble(stk.nextToken()));
				}
				
				//Read "</Angle>"
				else if ("Angle".equals(qName))
				{
					node.setAngle(Double.parseDouble(content.toString()));
				}
				
				//Read "</Elasticity>"
				else if ("Elasticity".equals(qName))
				{
					matIso.setMaterialValues(matIso.ELASTICITY,Double.parseDouble(content.toString()));
				}
				
				//Read "</Poisson>"
				else if ("Poisson".equals(qName))
				{
					matIso.setMaterialValues(matIso.POISSON,Double.parseDouble(content.toString()));
				}
				
				//Read "</Area>"
				else if ("Area".equals(qName))
				{
					cs.setArea(Double.parseDouble(content.toString()));
				}
				
				//Read "</Ix>"
				else if ("Ix".equals(qName))
				{
					cs.setIx(Double.parseDouble(content.toString()));
				}
				
				//Read "</Incidence>"
				else if ("Incidence".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					int i = 0;
					while (stk.hasMoreTokens())
					{
						elm.setNode(femModel.getNode(stk.nextToken()),i);
						i++;
					}
				}
				
				//Read "</Thickness>"
				else if ("Thickness".equals(qName))
				{
					cs.setLabel("cs_"+content.toString());
					cs.setThickness(Double.parseDouble(content.toString()));
					elm.setCrossSection(cs);
					femModel.add(cs);
				}
				
				//Read "</IntegrationOrder>"
				else if ("IntegrationOrder".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					intOr = new IntegrationOrder((int)(Double.parseDouble(stk.nextToken())),(int)(Double.parseDouble(stk.nextToken())),(int)(Double.parseDouble(stk.nextToken())));
					femModel.add(intOr);
					elm.setIntegrationOrder(femModel.getIntegrationOrder(content.toString()));
					elm.generateIntegrationPoints();
				}
				
				//Read "</MyMaterial>"
				else if ("MyMaterial".equals(qName))
				{
					elm.setMaterial(femModel.getMaterial(content.toString()));
				}
				
				//Read "</MyCrossSection>"
				else if ("MyCrossSection".equals(qName))
				{
					elm.setCrossSection(femModel.getCrossSection(content.toString()));
				}
				
				//Read "</MyAnalysisModel>"
				else if ("MyAnalysisModel".equals(qName))
				{
					if (content.toString().equals("Line_1D"))
					{
						 anl = new Line_1D();
					}
					if (content.toString().equals("Line_2D"))
					{
						 anl = new Line_2D();
					}
					if (content.toString().equals("Line_3D"))
					{
						 anl = new Line_3D();
					}
					if (content.toString().equals("PlaneStrainAnalysisM"))
					{
						 anl = new PlaneStrainAnalysisM();
					}
					if (content.toString().equals("PlaneStressAnalysisM"))
					{
						 anl = new PlaneStressAnalysisM();
					}
					if (content.toString().equals("AxiSymetricAnalysisM"))
					{
						 anl = new AxiSymetricAnalysisM();
					}
					if (content.toString().equals("SolidAnalysisM"))
					{
						 anl = new SolidAnalysisM();
					}
					femModel.add(anl);
					elm.setAnalysisModel(femModel.getAnalysisModel(content.toString()));
				}
				
				//Read "</Liberations>"
				else if ("Liberations".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(0, true);
//					else elm.setLiberation(0, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(1, true);
//					else elm.setLiberation(1, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(2, true);
//					else elm.setLiberation(2, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(3, true);
//					else elm.setLiberation(3, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(4, true);
//					else elm.setLiberation(4, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(5, true);
//					else elm.setLiberation(5, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(6, true);
//					else elm.setLiberation(6, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(7, true);
//					else elm.setLiberation(7, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(8, true);
//					else elm.setLiberation(8, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(9, true);
//					else elm.setLiberation(9, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(10, true);
//					else elm.setLiberation(10, false);
					
//					if (stk.nextToken().toLowerCase().equals("true"))
//						elm.setLiberation(11, true);
//					else elm.setLiberation(11, false);
					
				}
				
				//Read "</MyPointForce ...>"
				else if ("MyPointForce".equals(qName))
				{
					elm.addElmPointForce(pf);
				}
				
				//Read "</MyLineForce ...>"
				else if ("MyLineForce".equals(qName))
				{
				}
				
				//Read "</MySurfaceForce ...>"
				else if ("MySurfaceForce".equals(qName))
				{
				}
				
				//Read "</MyBodyForce ...>"
				else if ("MyVolumeForce".equals(qName))
				{
				}
				
				//Read "</LineElementForce ...>"
				else if ("LineElementForce".equals(qName))
				{
					elm.addElmLineForce(ef);
System.out.println("adicionou o LineelementForce na lista do elemento finito");
				}
				
				//Read "</SurfaceElementForce ...>"
				else if ("SurfaceElementForce".equals(qName))
				{
					elm.addElmSurfaceForce(ef);
System.out.println("adicionou o SurfaceelementForce na lista do elemento finito");
				}
				
				//Read "</VolumeElementForce ...>"
				else if ("VolumeElementForce".equals(qName))
				{
					elm.addElmVolumeForce(ef);
System.out.println("adicionou o VolumeelementForce na lista do elemento finito");
				}
				
				//Read "</PointForce ...>"
				else if ("PointForce".equals(qName))
				{
					ef.add(pf);
				}
				
				//Read "</PointForceCoords ...>"
				else if ("PointForceCoords".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					coord.x = Double.parseDouble(stk.nextToken());
					coord.y = Double.parseDouble(stk.nextToken());
					if (stk.hasMoreTokens())
						coord.z = Double.parseDouble(stk.nextToken());
					pf.setCoord(coord);
System.out.println("preencheu coords");
				}
				
				//Read "</Force ...>"
				else if ("Force".equals(qName))
				{
					stk = new StringTokenizer(content.toString(), " ");
					pf.setForce(0, Double.parseDouble(stk.nextToken()));
					pf.setForce(1, Double.parseDouble(stk.nextToken()));
					pf.setForce(2, Double.parseDouble(stk.nextToken()));
					pf.setForce(3, Double.parseDouble(stk.nextToken()));
					pf.setForce(4, Double.parseDouble(stk.nextToken()));
					pf.setForce(5, Double.parseDouble(stk.nextToken()));
System.out.println("preencheu forces");
				}
				
				//Read "</GlobalAnalysisModel>"
				else if ("GlobalAnalysisModel".equals(qName))
				{
					femModel.setGlobalAnalysisModel(femModel.getAnalysisModel(content.toString()));
				}
				
				//Read "</Driver>"
				else if ("Driver".equals(qName))
				{
					driver = new StructuralMech();
					driver.setFemModel(femModel);
				}
				
				//Read "</Solution>"
				/*else if ("Solution".equals(qName))
				{
					if (content.toString().equals("OnePointEq"))
					{
						 solution = new LinearEquationSystems();
					}
				}*/
				content = new StringBuffer();
//*/				
			}  // colchete de endElement
	});
} //colchete do try
	catch (ParserConfigurationException pce) 
	{
		System.out.println("Could not create that parser.");
		System.out.println(pce.getMessage());
		
		System.exit(1);
	}
	catch (SAXException se)
	{
		System.out.println("Problem with the SAX parser.");
		System.out.println(se.getMessage());
		System.exit(2);
	}
	catch (IOException ioe)
	{
		System.out.println("Error reading file.");
		System.out.println(ioe.getMessage());
		System.exit(3);
	}
	}

//*****************************************************************************
	
	/** Builds the DOM tree from the Driver.*/
	public static void buildDOMTreeFromDriver()
	{
		try
		{
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.newDocument();
			
			//Creating formator
			DecimalFormat fmt = new DecimalFormat();
			DecimalFormatSymbols fsym = new DecimalFormatSymbols();
			fsym.setDecimalSeparator('.');
			fmt.setDecimalFormatSymbols(fsym);
			
			//All the objects which will be serializes are referenced here
//			Solution solution = driver.getSolution();
			FemModel femModel = (FemModel)driver.getFemModel();
			Node node;
			Material mat;
			CrossSection cs;
			LinearElasticIsotropic matIso;
			ParametricElement elm;
			AnalysisModel anl;
			IntegrationPoint ip;
			
			//Creating root DOM element "JMefData"
			org.w3c.dom.Element root = doc.createElement("JMefData");
			doc.appendChild(root);
			
			//Creating DOM element "Driver"
			org.w3c.dom.Element elmDriver = doc.createElement("Driver");
			elmDriver.setAttribute("class", "" + driver.getClass());
			root.appendChild(elmDriver);
			
			//Creating DOM element "Solution"
			/*org.w3c.dom.Element elmSolution = doc.createElement("Solution");
			elmSolution.setAttribute("class", "" + solution.getClass());
			elmDriver.appendChild(elmSolution);*/
			
			//Creating DOM element "Model"
			org.w3c.dom.Element elmModel = doc.createElement("FemModel");
			elmModel.setAttribute("class", "" + femModel.getClass());
			elmDriver.appendChild(elmModel);
			
			//Creating DOM element "NumberOfEquations"
			org.w3c.dom.Element elmNumEquations = doc.createElement("NumberOfEquations");
			elmNumEquations.appendChild(doc.createTextNode(String.valueOf(femModel.getNumberOfEquations())));
			elmModel.appendChild(elmNumEquations);
			
			//Creating DOM element "NumberOfRestraints"
			org.w3c.dom.Element elmNumRestraints = doc.createElement("NumberOfRestraints");
			elmNumRestraints.appendChild(doc.createTextNode(String.valueOf(femModel.getNumberOfRestraints())));
			elmModel.appendChild(elmNumRestraints);
			
			//Creating DOM element "NodeList"
			org.w3c.dom.Element elmNodeList = doc.createElement("NodeList");
			elmModel.appendChild(elmNodeList);
			
			//Creating DOM element "MaterialList"
			org.w3c.dom.Element elmMaterialList = doc.createElement("MaterialList");
			elmModel.appendChild(elmMaterialList);
			
			//Creating DOM element "CrossSectionList"
			if(femModel.getNumberOfSections() != 0)
			{
				org.w3c.dom.Element elmCrossSectionList = doc.createElement("CrossSectionList");
				elmModel.appendChild(elmCrossSectionList);
				
				//Creating DOM elements "CrossSection"
				ListIterator css = femModel.getSectionsList().listIterator();
				while (css.hasNext())
				{
					cs = (CrossSection) css.next();
					org.w3c.dom.Element elmCrossSection = doc.createElement("CrossSection");
					elmCrossSection.setAttribute("class", "" + cs.getClass());
					elmCrossSection.setAttribute("label", "" + cs.getLabel());
					elmCrossSectionList.appendChild(elmCrossSection);
					
					//Creating DOM element "Area"
					if (cs.getArea() != 0)
					{
						org.w3c.dom.Element elmArea = doc.createElement("Area");
						elmArea.appendChild(doc.createTextNode(String.valueOf(cs.getArea())));
//						if (cs.getArea() != 0)
						elmCrossSection.appendChild(elmArea);
					}
					//Creating DOM element "Thickness"
					if (cs.getThickness() != 0)
					{
						org.w3c.dom.Element elmThickness = doc.createElement("Thickness");
						elmThickness.appendChild(doc.createTextNode(String.valueOf(cs.getThickness())));
						elmCrossSection.appendChild(elmThickness);
					}
				}
			}//End of creating DOM element "CrossSectionList"
			
			//Creating DOM element "AnalysisModelList"
			org.w3c.dom.Element elmAnalysisModelList = doc.createElement("AnalysisModelList");
			elmModel.appendChild(elmAnalysisModelList);
			
			//Creating DOM element "ElementList"
			org.w3c.dom.Element elmElementList = doc.createElement("ElementList");
			elmModel.appendChild(elmElementList);
			
			//Creating DOM element "GlobalAnalysisModel"
			org.w3c.dom.Element elmGlobalAnalysisModel = doc.createElement("GlobalAnalysisModel");
			elmGlobalAnalysisModel.appendChild(doc.createTextNode(String.valueOf(femModel.getGlobalAnalysisModel().getType())));
			elmModel.appendChild(elmGlobalAnalysisModel);
			
			//Creating DOM elements "Node"
			ListIterator nodes = femModel.getNodesList().listIterator();
			while (nodes.hasNext())
			{
				node = (Node)nodes.next();
				org.w3c.dom.Element elmNode = doc.createElement("Node");
				elmNode.setAttribute("label", "" + node.getLabel());
				elmNodeList.appendChild(elmNode);
				
				//Creating DOM elements "Coord"
				org.w3c.dom.Element elmCoord = doc.createElement("Coord");
				fmt.applyPattern("0.000E00");
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().x))));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().y))));
				elmCoord.appendChild(doc.createTextNode(" "));
				elmCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().z))));
				elmNode.appendChild(elmCoord);
				
				//Creating DOM elements "Restraints"
				org.w3c.dom.Element elmRestraints = doc.createElement("Restraints");
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(0))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(1))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(2))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(3))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(4))));
				elmRestraints.appendChild(doc.createTextNode(" "));
				elmRestraints.appendChild(doc.createTextNode(String.valueOf(node.getRestraint(5))));
				elmNode.appendChild(elmRestraints);
				
				//Creating DOM elements "Displacements"
				org.w3c.dom.Element elmDisplacements = doc.createElement("Displacements");
				fmt.applyPattern("0.000E00");
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(0)))));
				elmDisplacements.appendChild(doc.createTextNode(" "));
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(1)))));
				elmDisplacements.appendChild(doc.createTextNode(" "));
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(2)))));
				elmDisplacements.appendChild(doc.createTextNode(" "));
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(3)))));
				elmDisplacements.appendChild(doc.createTextNode(" "));
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(4)))));
				elmDisplacements.appendChild(doc.createTextNode(" "));
				elmDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getDisplacement(5)))));
				elmNode.appendChild(elmDisplacements);
				
				//Creating DOM elements "Load"
				org.w3c.dom.Element elmLoad = doc.createElement("Load");
				fmt.applyPattern("####0.000");
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(0)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(1)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(2)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(3)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(4)))));
				elmLoad.appendChild(doc.createTextNode(" "));
				elmLoad.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getForce(5)))));
				elmNode.appendChild(elmLoad);
				
				//Creating DOM elements "PreDisplacement"
				org.w3c.dom.Element elmPreDisplacements = doc.createElement("PreDisplacements");
				fmt.applyPattern("0.000E00");
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(0)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(1)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(2)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(3)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(4)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmPreDisplacements.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getPreDisplacement(5)))));
				elmPreDisplacements.appendChild(doc.createTextNode(" "));
				elmNode.appendChild(elmPreDisplacements);
				
				//Creating DOM element "mySpring"
				org.w3c.dom.Element elmMySpring = doc.createElement("mySpring");
				fmt.applyPattern("0.000E00");
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(0)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(1)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(2)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(3)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(4)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmMySpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getSpring(5)))));
				elmMySpring.appendChild(doc.createTextNode(" "));
				elmNode.appendChild(elmMySpring);
				
				//Creating DOM element "ForceOnSpring"
				org.w3c.dom.Element elmForceOnSpring = doc.createElement("ForceOnSpring");
				fmt.applyPattern("####0.000");
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(0)*node.getSpring(0)))));
				elmForceOnSpring.appendChild(doc.createTextNode(" "));
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(1)*node.getSpring(1)))));
				elmForceOnSpring.appendChild(doc.createTextNode(" "));
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(2)*node.getSpring(2)))));
				elmForceOnSpring.appendChild(doc.createTextNode(" "));
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(3)*node.getSpring(3)))));
				elmForceOnSpring.appendChild(doc.createTextNode(" "));
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(4)*node.getSpring(4)))));
				elmForceOnSpring.appendChild(doc.createTextNode(" "));
				elmForceOnSpring.appendChild(doc.createTextNode(String.valueOf(fmt.format(-node.getDisplacement(5)*node.getSpring(5)))));
				elmNode.appendChild(elmForceOnSpring);
				
				//Creating DOM elements "Reactions"
				org.w3c.dom.Element elmReactions = doc.createElement("Reactions");
				fmt.applyPattern("####0.000");
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(0)))));
				elmReactions.appendChild(doc.createTextNode(" "));
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(1)))));
				elmReactions.appendChild(doc.createTextNode(" "));
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(2)))));
				elmReactions.appendChild(doc.createTextNode(" "));
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(3)))));
				elmReactions.appendChild(doc.createTextNode(" "));
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(4)))));
				elmReactions.appendChild(doc.createTextNode(" "));
				elmReactions.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getReaction(5)))));
				elmNode.appendChild(elmReactions);
				
				//Creating DOM element "Angle"
				org.w3c.dom.Element elmAngle = doc.createElement("Angle");
				elmAngle.appendChild(doc.createTextNode(String.valueOf(node.getAngle())));
				elmNode.appendChild(elmAngle);
				
			}//End of creating DOM element "Node"
			
			//Creating DOM elements "Material"
			ListIterator mats = femModel.getMaterialsList().listIterator();
			while (mats.hasNext())
			{
				mat = (Material) mats.next();
				org.w3c.dom.Element elmMaterial = doc.createElement("Material");
				elmMaterial.setAttribute("class", "" + mat.getClass());
				elmMaterial.setAttribute("label", "" + mat.getLabel());
				elmMaterialList.appendChild(elmMaterial);
				
				//If Isotropic material
				if (mat.getLabel().equals("LinearElasticIsotropic"))
				{
					matIso = (LinearElasticIsotropic) mat;
					
					//Creating DOM element "Elasticity"
					org.w3c.dom.Element elmElasticity = doc.createElement("Elasticity");
					elmElasticity.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.ELASTICITY).doubleValue())));
					if (matIso.getMaterialValues(matIso.ELASTICITY).doubleValue()!=0)
						elmMaterial.appendChild(elmElasticity);
					
					//Creating DOM element "Poisson"
					org.w3c.dom.Element elmPoisson = doc.createElement("Poisson");
					elmPoisson.appendChild(doc.createTextNode(String.valueOf(matIso.getMaterialValues(matIso.POISSON).doubleValue())));
					if (matIso.getMaterialValues(matIso.POISSON).doubleValue()!=0)
						elmMaterial.appendChild(elmPoisson);
				}
				
				//If MSpring material
//				else if (mat.getType().equals("MSpring"))
//				{
//					matSpr = (MSpring) mat;
					
					//Creating DOM element "ka"
//					org.w3c.dom.Element elmKa = doc.createElement("ka");
//					elmKa.appendChild(doc.createTextNode(String.valueOf(matSpr.getKa())));
//					if (matSpr.getKa()!=0)
//						elmMaterial.appendChild(elmKa);
//				}
				
			}//End of creating DOM element "Material"
			
			//Creating DOM elements "AnalysisModel"
			ListIterator anls = femModel.getAnalysisModelsList().listIterator();
			while (anls.hasNext())
			{
				anl = (AnalysisModel)anls.next();
				org.w3c.dom.Element elmAnalysisModel = doc.createElement("AnalysisModel");
				elmAnalysisModel.setAttribute("class", "" + anl.getClass());
				elmAnalysisModel.setAttribute("label", "" + anl.getType());
				elmAnalysisModelList.appendChild(elmAnalysisModel);
			}//End of creating DOM elements "AnalysisModel"
			
			//Creating DOM elements "Element"
			ListIterator elms = femModel.getElementsList().listIterator();
			while (elms.hasNext())
			{
				elm = (ParametricElement)elms.next();
				org.w3c.dom.Element elmElement = doc.createElement("Element");
				elmElement.setAttribute("class", "" + elm.getClass());
				elmElement.setAttribute("label", "" + elm.getLabel());
				elmElementList.appendChild(elmElement);
				
				//Creating DOM element "Incidence"
				org.w3c.dom.Element elmIncidence = doc.createElement("Incidence");
				int i = 0;
				ListIterator elementNodes = elm.getIncidence().listIterator();
				while (elementNodes.hasNext())
				{
					node = (Node)elementNodes.next();
					elmIncidence.appendChild(doc.createTextNode(String.valueOf(elm.getNode(i).getLabel())));
					if(elementNodes.hasNext())
					{
						elmIncidence.appendChild(doc.createTextNode(" "));
					}
					elmElement.appendChild(elmIncidence);
					i++;
				}
				
				//Creating DOM element "IntegrationOrder"
				org.w3c.dom.Element elmIntegOrder = doc.createElement("IntegrationOrder");
				elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(elm.getIntegrationOrder().getXiIntegOrder())));
				elmIntegOrder.appendChild(doc.createTextNode(" "));
				elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(elm.getIntegrationOrder().getEtaIntegOrder())));
				elmIntegOrder.appendChild(doc.createTextNode(" "));
				elmIntegOrder.appendChild(doc.createTextNode(String.valueOf(elm.getIntegrationOrder().getZetaIntegOrder())));
				elmElement.appendChild(elmIntegOrder);
				
				//Creating DOM element "MyMaterial"
				org.w3c.dom.Element elmMyMaterial = doc.createElement("MyMaterial");
				elmMyMaterial.appendChild(doc.createTextNode(elm.getMaterial().getLabel()));
				elmElement.appendChild(elmMyMaterial);

				//os elementos axisymetricos nao possuem espessura.
				//area E secao iguais a zero somente para elementos solidos
				if((elm.getAnalysisModel().getType().equals("SolidAnalysisM")) || (elm.getAnalysisModel().getType().equals("AxiSymetricAnalysisM")))
				{
				}
				else
				{
					//Creating DOM element "MyCrossSection"
					org.w3c.dom.Element elmMyCrossSection = doc.createElement("MyCrossSection");
					elmMyCrossSection.appendChild(doc.createTextNode(elm.getCrossSection().getLabel()));
					elmElement.appendChild(elmMyCrossSection);
				}
				
				//Creating DOM element "MyAnalysisModel"
				org.w3c.dom.Element elmMyAnalysisModel = doc.createElement("MyAnalysisModel");
				elmMyAnalysisModel.appendChild(doc.createTextNode(elm.getAnalysisModel().getType()));
				elmElement.appendChild(elmMyAnalysisModel);
				
				//Creating DOM element "GaussPointsList"
				org.w3c.dom.Element elmGaussPointsList = doc.createElement("GaussPointList");
				
				//Creating DOM element "GaussPointResults"
				org.w3c.dom.Element elmGaussPointResults = doc.createElement("GaussPointResults");
				anl = elm.getAnalysisModel();
				elmGaussPointsList.appendChild(elmGaussPointResults);
				
				//Creating DOM element "GPCoords"
				org.w3c.dom.Element elmGPCoords = doc.createElement("GPCartesianCoords");
				elmGPCoords.appendChild(doc.createTextNode("x y z"));
				elmGaussPointResults.appendChild(elmGPCoords);
				
				//Creating DOM element "GPStrains"
				org.w3c.dom.Element elmGPStrains = doc.createElement("GPStrains");
				elmGPStrains.appendChild(doc.createTextNode(anl.getStrainLabels()));
				elmGaussPointResults.appendChild(elmGPStrains);
				
				//Creating DOM element "GPStresses"
				org.w3c.dom.Element elmGPStresses = doc.createElement("GPStresses");
				elmGPStresses.appendChild(doc.createTextNode(anl.getStressLabels()));
				elmGaussPointResults.appendChild(elmGPStresses);
				
				//Creating DOM elements "GaussPoint"
				ListIterator ips = elm.getIntegrationPointsList().listIterator();
				mat = elm.getMaterial();
				while(ips.hasNext())
				{
					ip = (IntegrationPoint) ips.next();
					
					org.w3c.dom.Element elmGaussPoint = doc.createElement("GaussPoint");
					elmGaussPoint.setAttribute("label", "" + ip.getLabel());
					elmGaussPointsList.appendChild(elmGaussPoint);
					
					//Creating DOM elements "GPCartesianCoord"
					org.w3c.dom.Element elmGPCoord = doc.createElement("GPCartesianCoord");
					fmt.applyPattern("0.0000E00");
					Shape shp = elm.getShape();
					IVector Ni = shp.getShapeFunction(ip.getCoords());
					int nNos = elm.getIncidence().size();
					IVector xi = new IVector(nNos);
					IVector yi = new IVector(nNos);
					IVector zi = new IVector(nNos);
					ListIterator elmNodes = elm.getIncidence().listIterator();
					int j = 0;
//System.out.println("ip.getCoords(): "+prov[0]+", "+prov[1]);
					while (elmNodes.hasNext())
					{
						node = (Node)elmNodes.next();
						xi.setElement(j,(node.getCoord()).x);
						yi.setElement(j,(node.getCoord()).y);
						zi.setElement(j,(node.getCoord()).z);
						j++;
					}
//System.out.println("Vetor xi: ");
//imprimeVetor(xi);
					elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(xi)))));
					elmGPCoord.appendChild(doc.createTextNode(" "));
					elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(yi)))));
					elmGPCoord.appendChild(doc.createTextNode(" "));
					elmGPCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(Ni.dot(zi)))));
					elmGaussPoint.appendChild(elmGPCoord);
					
					//Creating DOM elements "GPStrain"
					org.w3c.dom.Element elmGPStrain = doc.createElement("GPStrain");
					IVector strains = anl.getStrainVector(elm.getPointStrains(ip.getCoords()), mat);
					fmt.applyPattern("0.000E00");
					for(int m = 0; m < (strains.getSize()); m++)
					{
						elmGPStrain.appendChild(doc.createTextNode(String.valueOf(fmt.format(strains.getElement(m)))));
						if(m < (strains.getSize()-1))//para retirar o espaco no final da tag
						elmGPStrain.appendChild(doc.createTextNode(" "));
					}
					elmGaussPoint.appendChild(elmGPStrain);
					
					//Creating DOM elements "GPStress"
					org.w3c.dom.Element elmGPStress = doc.createElement("GPStress");
					IVector stresses = anl.getStressVector(elm.getPointStress(ip.getCoords()), mat);
					fmt.applyPattern("0.000000E00");
					for(int k = 0; k < (stresses.getSize()); k++)
					{
						elmGPStress.appendChild(doc.createTextNode(String.valueOf(fmt.format(stresses.getElement(k)))));
						if(k < (stresses.getSize()-1))//para retirar o espaco no final da tag
						elmGPStress.appendChild(doc.createTextNode(" "));
					}
					elmGaussPoint.appendChild(elmGPStress);
				}
				elmElement.appendChild(elmGaussPointsList);
				
				//Creating DOM element "NodalStressesAndStrains"
				org.w3c.dom.Element elmNodalResults = doc.createElement("NodalStressesAndStrains");
				anl = elm.getAnalysisModel();
				IMatrix stressesAtNode = elm.getNodalPointStress();
				IMatrix strainsAtNode = elm.getNodalPointStrains();
				IVector aux = new IVector(stressesAtNode.getNumRow());
				IVector aux1 = new IVector(strainsAtNode.getNumRow());
				int m = 0;//contador usado nas deformacoes nodais
				int n = 0;//contador usado nas tensoes nodais
				
				//Creating DOM element "NodeCoords"
				org.w3c.dom.Element elmNodeCoords = doc.createElement("NodeCoords");
				elmNodeCoords.appendChild(doc.createTextNode("x y z"));
				elmNodalResults.appendChild(elmNodeCoords);
				
				//Creating DOM element "NodeStrains"
				org.w3c.dom.Element elmNodeStrains = doc.createElement("NodeStrains");
				elmNodeStrains.appendChild(doc.createTextNode(anl.getStrainLabels()));
				elmNodalResults.appendChild(elmNodeStrains);
				
				//Creating DOM element "NodeStresses"
				org.w3c.dom.Element elmNodeStresses = doc.createElement("NodeStresses");
				elmNodeStresses.appendChild(doc.createTextNode(anl.getStressLabels()));
				elmNodalResults.appendChild(elmNodeStresses);
				
				ListIterator nds = elm.getIncidence().listIterator();
				while(nds.hasNext())
				{
					node = (Node) nds.next();
					
					org.w3c.dom.Element elmNodeLabel = doc.createElement("Node");
					elmNodeLabel.setAttribute("label", "" + node.getLabel());
					elmNodalResults.appendChild(elmNodeLabel);
					
					//Creating DOM elements "NodeCoord"
					org.w3c.dom.Element elmNodeCoord = doc.createElement("NodeCoord");
					fmt.applyPattern("0.000E00");
					elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().x))));
					elmNodeCoord.appendChild(doc.createTextNode(" "));
					elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().y))));
					elmNodeCoord.appendChild(doc.createTextNode(" "));
					elmNodeCoord.appendChild(doc.createTextNode(String.valueOf(fmt.format(node.getCoord().z))));
					elmNodalResults.appendChild(elmNodeCoord);
					
					//Creating DOM elements "NodeStrain"
					org.w3c.dom.Element elmNodeStrain = doc.createElement("NodeStrain");
					fmt.applyPattern("0.000E00");
					strainsAtNode.getColumn(m, aux1);
					for (int p = 0; p < (aux1.getSize()); p++)
					{
						elmNodeStrain.appendChild(doc.createTextNode(String.valueOf(fmt.format(aux1.getElement(p)))));
						if(p < (aux1.getSize()-1))//para retirar o espaco no final da tag
						elmNodeStrain.appendChild(doc.createTextNode(" "));
					}
					m++;
					elmNodalResults.appendChild(elmNodeStrain);
					
					//Creating DOM elements "NodeStress"
					org.w3c.dom.Element elmNodeStress = doc.createElement("NodeStress");
					fmt.applyPattern("0.000E00");
					stressesAtNode.getColumn(n, aux);
					for (int k = 0; k < (aux.getSize()); k++)
					{
						elmNodeStress.appendChild(doc.createTextNode(String.valueOf(fmt.format(aux.getElement(k)))));
						if(k < (aux.getSize()-1))//para retirar o espaco no final da tag
						elmNodeStress.appendChild(doc.createTextNode(" "));
					}
					n++;
					elmNodalResults.appendChild(elmNodeStress);
			}
			elmElement.appendChild(elmNodalResults);
				
				
		//AQUI TERMINA O QUE JA IMPLEMENTEI
				
				//Creating DOM element "Liberations"
//				org.w3c.dom.Element elmLiberations = doc.createElement("Liberations");
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(0))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(1))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(2))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(3))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(4))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(5))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(6))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(7))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(8))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(9))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(10))));
//				elmLiberations.appendChild(doc.createTextNode(" "));
//				elmLiberations.appendChild(doc.createTextNode(String.valueOf(elm.getLiberation(11))));
//				elmElement.appendChild(elmLiberations);
				
				//Creating DOM element "MyElmPointForce"
//				org.w3c.dom.Element elmMyElmPointForce = doc.createElement("MyElmPointForce");
//				elmElement.appendChild(elmMyElmPointForce);
				
				//Creating DOM elements "ElmPointForce"
//				ListIterator pfs = elm.getElmPointForcesList().listIterator();
//				while (pfs.hasNext())
//				{
//					pf = (PointForce)pfs.next();
//					org.w3c.dom.Element elmPointForce = doc.createElement("ElmPointForce");
//					elmMyElmPointForce.appendChild(elmPointForce);
					
					//Creating DOM element "Point"
//					org.w3c.dom.Element elmPoint = doc.createElement("Point");
//					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().x)));
//					elmPoint.appendChild(doc.createTextNode(" "));
//					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().y)));
//					elmPoint.appendChild(doc.createTextNode(" "));
//					elmPoint.appendChild(doc.createTextNode(String.valueOf(pf.getCoord().z)));
//					elmPointForce.appendChild(elmPoint);
					
					//Creating DOM element "Force"
//					org.w3c.dom.Element elmForce = doc.createElement("Force");
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(0))));
//					elmForce.appendChild(doc.createTextNode(" "));
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(1))));
//					elmForce.appendChild(doc.createTextNode(" "));
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(2))));
//					elmForce.appendChild(doc.createTextNode(" "));
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(3))));
//					elmForce.appendChild(doc.createTextNode(" "));
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(4))));
//					elmForce.appendChild(doc.createTextNode(" "));
//					elmForce.appendChild(doc.createTextNode(String.valueOf(pf.getForce(5))));
//					elmPointForce.appendChild(elmForce);
					
//				}//End of creating DOM elements "Element"
				
				//Creating DOM element "MyBodyForce"
//				org.w3c.dom.Element elmMyBodyForce = doc.createElement("MyBodyForce");
//				elmElement.appendChild(elmMyBodyForce);
				
				//Creating DOM element "MyBodyForce"
//				ListIterator bfs = elm.getBodyForcesList().listIterator();
//				while (bfs.hasNext())
//				{
//					bf = (BodyForce)bfs.next();
//					org.w3c.dom.Element elmBodyForce = doc.createElement("BodyForce");
//					elmMyBodyForce.appendChild(elmBodyForce);
					
					//Creating DOM element "InitialCoords"
//					org.w3c.dom.Element elmInitialCoords = doc.createElement("InitialCoords");
//					elmInitialCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPi().x)));
//					elmInitialCoords.appendChild(doc.createTextNode(" "));
//					elmInitialCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPi().y)));
//					elmInitialCoords.appendChild(doc.createTextNode(" "));
//					elmInitialCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPi().z)));
//					elmBodyForce.appendChild(elmInitialCoords);
					
					//Creating DOM element "FinalCoords"
//					org.w3c.dom.Element elmFinalCoords = doc.createElement("FinalCoords");
//					elmFinalCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPf().x)));
//					elmFinalCoords.appendChild(doc.createTextNode(" "));
//					elmFinalCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPf().y)));
//					elmFinalCoords.appendChild(doc.createTextNode(" "));
//					elmFinalCoords.appendChild(doc.createTextNode(String.valueOf(bf.getPf().z)));
//					elmBodyForce.appendChild(elmFinalCoords);
					
					//Creating DOM element "InitialForce"
//					org.w3c.dom.Element elmInitialForce = doc.createElement("InitialForce");
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(0))));
//					elmInitialForce.appendChild(doc.createTextNode(" "));
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(1))));
//					elmInitialForce.appendChild(doc.createTextNode(" "));
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(2))));
//					elmInitialForce.appendChild(doc.createTextNode(" "));
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(3))));
//					elmInitialForce.appendChild(doc.createTextNode(" "));
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(4))));
//					elmInitialForce.appendChild(doc.createTextNode(" "));
//					elmInitialForce.appendChild(doc.createTextNode(String.valueOf(bf.getPiForce().getForce(5))));
//					elmBodyForce.appendChild(elmInitialForce);
					
					//Creating DOM element "FinalForce"
//					org.w3c.dom.Element elmFinalForce = doc.createElement("FinalForce");
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(0))));
//					elmFinalForce.appendChild(doc.createTextNode(" "));
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(1))));
//					elmFinalForce.appendChild(doc.createTextNode(" "));
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(2))));
//					elmFinalForce.appendChild(doc.createTextNode(" "));
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(3))));
//					elmFinalForce.appendChild(doc.createTextNode(" "));
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(4))));
//					elmFinalForce.appendChild(doc.createTextNode(" "));
//					elmFinalForce.appendChild(doc.createTextNode(String.valueOf(bf.getPfForce().getForce(5))));
//					elmBodyForce.appendChild(elmFinalForce);
				
//				}//End of creating DOM element MyBodyForce
				
				//Creating DOM element "ActionsAtExtremities"
//				org.w3c.dom.Element elmActionsAtExtremities = doc.createElement("ActionsAtExtremities");
//				fmt.applyPattern("####0.000");
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(0)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(1)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(2)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(3)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(4)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(5)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(6)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(7)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(8)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(9)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(10)))));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(" "));
//				elmActionsAtExtremities.appendChild(doc.createTextNode(String.valueOf(fmt.format(elm.getActionAtExtremity(11)))));
//				elmElement.appendChild(elmActionsAtExtremities);
				
			}//End of creating DOM elements "Element"
			
			docOutput = doc;
		} //end of try 
		catch (ParserConfigurationException pce)
		{
			System.out.println("Could not create DOM parser.");
			System.out.println(pce.getMessage());
			System.exit(4);
		}
	}
	
//*****************************************************************************
	
	/** Serializes the DOM tree into a XML file.
	*@param resultFile The name of the output file.
	*/
	public static void serializeXMLDocument(String resultFile)
	{
		try
		{
			Transformer trans = TransformerFactory.newInstance().newTransformer();
			trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "JMefData.dtd");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");
			trans.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1");
			trans.transform(new DOMSource((Document) docOutput ), new StreamResult(resultFile));
		} 
		catch (TransformerException te)
		{
			System.out.println("Problem serializing DOM tree");
			System.out.println(te.getMessageAndLocation());
			System.exit(5);
		}
	}
	
//********************************************************************************
	
	public static void imprimeMatriz(IMatrix a)
	{
		for (int i = 0; i < a.getNumRow(); i++)
		{
			System.out.println();
			for (int j = 0; j < a.getNumCol(); j++)
			{
				System.out.print(a.getElement(i, j));
				System.out.print("\t");
			}
		}
		System.out.println("TAMANHO DA MATRIZ: "+a.getNumRow()+" x "+a.getNumCol());
	}
	
	public static void imprimeVetor(IVector a)
	{
		for (int i=0; i<a.getSize(); i++)
		{
			System.out.println();
			System.out.print(a.getElement(i));
			System.out.print("\t");
		}
		System.out.println("TAMANHO DO VETOR: "+a.getSize());
	}
	
//*****************************************************************************

}
